﻿package core.image.encoder
{
    import flash.display.*;
    import flash.utils.*;

    public class BMPEncoder extends Object
    {
        public const BIT_DEPTH:uint = 24;

        public function BMPEncoder()
        {
            return;
        }// end function

        public function encode(core.image.encoder:BMPEncoder:BitmapData) : ByteArray
        {
            var _loc_2:* = new ByteArray();
            _loc_2.endian = "littleEndian";
            this.writeHeader(_loc_2);
            this.writeInfoHeader(_loc_2, core.image.encoder:BMPEncoder);
            this.writeData(_loc_2, core.image.encoder:BMPEncoder);
            this.setFileSize(_loc_2);
            return _loc_2;
        }// end function

        private function writeData(setFileSize:ByteArray, setFileSize:BitmapData) : void
        {
            var _loc_3:int = 0;
            var _loc_6:int = 0;
            var _loc_7:uint = 0;
            var _loc_4:* = setFileSize.width % 4;
            var _loc_5:* = new ByteArray();
            while (_loc_6 < _loc_4)
            {
                
                _loc_5.writeByte(0);
                _loc_6++;
            }
            var _loc_8:* = setFileSize.height - 1;
            while (_loc_8 >= 0)
            {
                
                _loc_3 = 0;
                while (_loc_3 < setFileSize.width)
                {
                    
                    _loc_7 = setFileSize.getPixel(_loc_3, _loc_8);
                    setFileSize.writeByte(255 & _loc_7);
                    setFileSize.writeByte((65280 & _loc_7) >> 8);
                    setFileSize.writeByte((16711680 & _loc_7) >> 16);
                    _loc_3++;
                }
                setFileSize.writeBytes(_loc_5, 0, _loc_4);
                _loc_8 = _loc_8 - 1;
            }
            return;
        }// end function

        private function writePixel(setFileSize:ByteArray, setFileSize:uint) : void
        {
            setFileSize.writeByte(255 & setFileSize);
            setFileSize.writeByte((65280 & setFileSize) >> 8);
            setFileSize.writeByte((16711680 & setFileSize) >> 16);
            return;
        }// end function

        private function writeHeader(setFileSize:ByteArray) : void
        {
            setFileSize.writeShort(19778);
            setFileSize.writeUnsignedInt(0);
            setFileSize.writeShort(0);
            setFileSize.writeShort(0);
            setFileSize.writeUnsignedInt(54);
            return;
        }// end function

        private function writeInfoHeader(setFileSize:ByteArray, setFileSize:BitmapData) : void
        {
            setFileSize.writeUnsignedInt(40);
            setFileSize.writeInt(setFileSize.width);
            setFileSize.writeInt(setFileSize.height);
            setFileSize.writeShort(1);
            setFileSize.writeShort(this.BIT_DEPTH);
            setFileSize.writeUnsignedInt(0);
            setFileSize.writeUnsignedInt(0);
            setFileSize.writeInt(0);
            setFileSize.writeInt(0);
            setFileSize.writeUnsignedInt(0);
            setFileSize.writeUnsignedInt(0);
            return;
        }// end function

        private function setFileSize(setFileSize:ByteArray) : void
        {
            var _loc_2:* = setFileSize.position;
            setFileSize.position = 2;
            setFileSize.writeUnsignedInt(setFileSize.length);
            setFileSize.position = _loc_2;
            return;
        }// end function

    }
}
